package config

import (
	"github.com/astaxie/beego/orm"
	"qycms/controllers"
	"qycms/models/set"
)

type ConfigCotroller struct {
	controllers.BaseController
}
//站点设置页面
func (this *ConfigCotroller) Index()  {
	result := []set.Config{}
	o := orm.NewOrm()
	o.QueryTable(new(set.Config)).All(&result)
	options := make(map[string]string)
	for _, v := range result {
		options[v.Name] = v.Value
	}
	this.Data["config"] = options
	this.TplName = "congig/index.html"
}

//更新站点设置
func (this *ConfigCotroller) UpdateSet()  {
	result := []*set.Config{}
	o := orm.NewOrm()
	o.QueryTable(new(set.Config)).All(&result)
	options := make(map[string]string)
	mp := make(map[string]*set.Config)
	for _, v := range result {
		options[v.Name] = v.Value
		mp[v.Name] = v
	}

	if this.Ctx.Request.Method == "POST" {
		keys := []string{"sitename", "domain", "seo", "keywords", "description", "address", "phone","copyright","icp"}
		for _, key := range keys {
			val := this.GetString(key)
			if _, ok := mp[key]; !ok {
				o.Insert(&set.Config{Name: key, Value: val})
			} else {
				opt := mp[key]
				if _, err := o.Update(&set.Config{Id: opt.Id, Name: opt.Name, Value: val}); err != nil {
					continue
				}
			}
		}
		this.Suc(nil)
	}
}